<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <el-card class="box-card">
        <div class="BigBox">
          <div class="Logo">
            <img src="http://www-wms-java.itheima.net/img/title.ef005a7a.png" alt="">
            <div>
              <el-form-item prop="username">
                <span class="svg-container">
                  <svg-icon icon-class="dashboard" class="SVG" />
                </span>
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="Username"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on"
                />
              </el-form-item>

              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" class="SVG" />
                </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="Password"
                  name="password"
                  tabindex="2"
                  auto-complete="on"
                  @keyup.enter.native="handleLogin"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                </span>
              </el-form-item>
            </div>
            <!-- 记住密码 -->
            <div class="checkedBox">
              <div>
                <el-checkbox v-model="checked">记住密码</el-checkbox>
              </div>
            </div>
            <!-- 登录按钮 -->
            <div class="LoginBtn">
              <el-button type="warning" @click="handleLogin">立即登录</el-button>
            </div>
          </div>
          <div class="RightImg">
            <img src="http://www-wms-java.itheima.net/img/contentBg.1321d126.png" alt="" style="width: 100%;">
          </div>
        </div>
      </el-card>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { setToken } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      // 记住密码
      checked: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$router.push({ path: this.redirect || '/' })
      setToken('eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJiNTJlYWUxMS0yYWZiLTQwYjUtODg3NC01OTEwOGU0YjI0NjUiLCJzdWIiOiJ7XCJyb2xlXCI6XCJST0xFX0FETUlOXCIsXCJ0eXBlXCI6XCIyXCIsXCJ1c2VybmFtZVwiOlwiYWRtaW5cIn0iLCJpc3MiOiJhZG1pbiIsImlhdCI6MTYwNjM3MTA3NiwiZXhwIjoxNjA2OTc1ODc2fQ.pCl45pY_v2w8rZU8l60UI4cQx-Ufl7DRw0rZqhY8zeE')
      // this.$refs.loginForm.validate(valid => {
      //   if (valid) {
      //     this.loading = true
      //     this.$store.dispatch('user/login', this.loginForm).then(() => {
      //       this.$router.push({ path: this.redirect || '/' })
      //       this.loading = false
      //     }).catch(() => {
      //       this.loading = false
      //     })
      //   } else {
      //     console.log('error submit!!')
      //     return false
      //   }
      // })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#1f2d3d;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 50%;
    line-height:50px;
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    input {
      background: transparent;
      border: 0px;
      width: 166%;
      height: 51px;
      // -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      color: $light_gray;
      vertical-align: middle;
      border-bottom-right-radius: 10px;
    border-top-right-radius: 10px;
      // caret-color: $cursor;
      transition: border-color .2s cubic-bezier(.645,.045,.355,1);
      // &:-webkit-autofill {
      //   box-shadow: 0 0 0px 1000px $bg inset !important;
      //   -webkit-text-fill-color: $cursor !important;
      // }
      &:hover{
        border:1px #333 solid;
        transition: border-color .2s cubic-bezier(.645,.045,.355,1);
      }
      &:focus{
        border:1px #ffb200 solid;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #f8f5f5;
    border-radius: 10px;
    color: #454545;
    width: 80%;
    margin:40px;
    margin-bottom: 10px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#ffffff;
$dark_gray:#c6bebe;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 958px;
    height: 600px;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    ::v-deep .el-card__body{
      padding: 0px 0px;
    }
    .box-card{
        position:absolute;
      top:50px;
      border-radius: 40px;
      height:500px;
      .checkedBox{
        margin-top: 22px;
      margin-left: 45px;
      }
      .LoginBtn{
        width: 300px;
        margin:40px;
        margin-top:15px;
        .el-button--warning{
          width:100%;
          height: 50px;
          background-color: #ffb300dc;
          color:#333;
          font-weight: 700;
          font-size: 16px;
          border-radius:8px;
          box-shadow: 1px 1px 10px rgba(158, 156, 156, 0.678);
        }
      }
    .BigBox{
      display: flex;
      .Logo{
      img{
        margin-left: 124px;
    margin-top: 72px;
    margin-right:100px;
      width: 150px;
      height: 64px;
      }
      }
        .RightImg {
          img{
              width: 100%;
              height:500px
          }
        }
      }
    }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 50px;
    line-height: 40px;
    display: inline-block;
    background-color: #eae7e7;
    border-bottom-left-radius: 10px;
    border-top-left-radius: 10px;
    .SVG{
      zoom: 150%;
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
</style>
